package com.atyjh.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atyjh.reggie.dto.Result;
import com.atyjh.reggie.dto.SetmealDto;
import com.atyjh.reggie.entity.Category;
import com.atyjh.reggie.entity.DishFlavor;
import com.atyjh.reggie.entity.Setmeal;
import com.atyjh.reggie.entity.SetmealDish;
import com.atyjh.reggie.mapper.SetMealMapper;
import com.atyjh.reggie.service.ICategoryService;
import com.atyjh.reggie.service.IDishFlavorService;
import com.atyjh.reggie.service.ISetMealDishService;
import com.atyjh.reggie.service.ISetMealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yjh
 * @since jdk1.8.0_311 2022.06.16
 */
@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements ISetMealService {

    @Resource
    private ICategoryService categoryService;
    @Resource
    private ISetMealDishService setMealDishService;

    @Resource
    private IDishFlavorService dishFlavorService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Page<SetmealDto>> pageMeal(Integer page, Integer pageSize, String name) {

//        1.1 构造分页构造器
        Page<Setmeal> setMealPage = new Page<>(page, pageSize);

//        1.2 构造条件构造器
        LambdaQueryWrapper<Setmeal> smlqw = new LambdaQueryWrapper<>();
        smlqw.orderByDesc(Setmeal::getUpdateTime);
        smlqw.like(name != null, Setmeal::getName, name);

//        1.3 得到分页信息
        page(setMealPage, smlqw);

        Page<SetmealDto> setmealDtoPage = new Page<>();
//        2.1 将分页信息拷贝到信息的分页构造器中（除了records字段）
        BeanUtil.copyProperties(setMealPage, setmealDtoPage, "records");
//        2.2 将获得分类名称的records赋给分页构造器
        List<Setmeal> records = setMealPage.getRecords();
        if (records != null) {
            List<SetmealDto> list = records.stream().map(item -> {
//                2.2.1 将SetMeal转化成SetMealDto
                SetmealDto setmealDto = new SetmealDto();
                BeanUtil.copyProperties(item, setmealDto);
//                2.2.2 设置categoryName字段
                Category category = categoryService.getById(item.getCategoryId());
                setmealDto.setCategoryName(category.getName());
                return setmealDto;
            }).collect(Collectors.toList());
            setmealDtoPage.setRecords(list);
        }
        return Result.success(setmealDtoPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> saveMeal(SetmealDto setmealDto) {

        Setmeal setmeal = new Setmeal();
//        保存套餐主信息（除了菜品信息）
        BeanUtil.copyProperties(setmealDto, setmeal);
        save(setmeal);
//        保存菜品信息（先设置菜品的套餐id）
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes().stream().map(item -> {
            item.setSetmealId(setmeal.getId());
            return item;
        }).collect(Collectors.toList());
        setMealDishService.saveBatch(setmealDishes);

        return Result.success("保存成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> delete(List<Long> ids) {

//        查询套餐是否正在售卖
        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        slqw.in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1);
        long count = count(slqw);
        if (count > 0) {
            return Result.error("套餐正在售卖！不能删除");
        }

//        删除套餐
        removeBatchByIds(ids);

//        删除套餐关联的菜品
        LambdaQueryWrapper<SetmealDish> sdlqw = new LambdaQueryWrapper<>();
        sdlqw.in(SetmealDish::getSetmealId, ids);
        setMealDishService.remove(sdlqw);

        return Result.success("删除成功！");
    }

    @Override
    public Result<List<Setmeal>> listDish(Long categoryId, String status) {

        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        slqw.eq(Setmeal::getCategoryId, categoryId);
        slqw.eq(Setmeal::getStatus, status);
        List<Setmeal> setmealList = list(slqw);

        return Result.success(setmealList);

    }


}
